package com.central.modules.job.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.central.common.annotation.LogOperation;
import com.central.common.page.PageData;
import com.central.common.utils.ConvertUtils;
import com.central.common.utils.Result;
import com.central.common.validator.ValidatorUtils;
import com.central.common.validator.group.AddGroup;
import com.central.common.validator.group.DefaultGroup;
import com.central.common.validator.group.UpdateGroup;
import com.central.modules.job.dto.ScheduleJobDTO;
import com.central.modules.job.entity.ScheduleJobEntity;
import com.central.modules.job.model.ScheduleJobQueryBean;
import com.central.modules.job.service.ScheduleJobService;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
@AllArgsConstructor
public class ScheduleJobController {
    private final ScheduleJobService scheduleJobService;

    @GetMapping("page")
    @RequiresPermissions("sys:schedule:page")
    public Result<PageData<ScheduleJobDTO>> page(ScheduleJobQueryBean queryBean) {
        IPage<ScheduleJobEntity> iPage = scheduleJobService.pageSearch(queryBean);
        return Result.succeed(PageData.copy(iPage, ConvertUtils.sourceToTarget(iPage.getRecords(), ScheduleJobDTO.class)));
    }

    @GetMapping("{id}")
    @RequiresPermissions("sys:schedule:info")
    public Result<ScheduleJobDTO> info(@PathVariable("id") Long id) {
        ScheduleJobEntity schedule = scheduleJobService.getEntityById(id);
        return Result.succeed(ConvertUtils.sourceToTarget(schedule, ScheduleJobDTO.class));
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("sys:schedule:save")
    public Result<Void> save(@RequestBody ScheduleJobDTO dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        scheduleJobService.add(ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class));
        return Result.succeed(null);
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("sys:schedule:update")
    public Result<Void> update(@RequestBody ScheduleJobDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        scheduleJobService.update(ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class));
        return Result.succeed(null);
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("sys:schedule:delete")
    public Result<Void> delete(@RequestBody Long[] ids) {
        scheduleJobService.deleteBatch(ids);

        return Result.succeed(null);
    }

    @PutMapping("/run")
    @LogOperation("立即执行")
    @RequiresPermissions("sys:schedule:run")
    public Result<Void> run(@RequestBody Long[] ids) {
        scheduleJobService.run(ids);

        return Result.succeed(null);
    }

    @PutMapping("/pause")
    @LogOperation("暂停")
    @RequiresPermissions("sys:schedule:pause")
    public Result<Void> pause(@RequestBody Long[] ids) {
        scheduleJobService.pause(ids);

        return Result.succeed(null);
    }

    @PutMapping("/resume")
    @LogOperation("恢复")
    @RequiresPermissions("sys:schedule:resume")
    public Result<Void> resume(@RequestBody Long[] ids) {
        scheduleJobService.resume(ids);
        return Result.succeed(null);
    }

}